/**
 * Created by lwy on 2017/7/31.
 */


var page = 1;
var id;
var password;
var userkey;
function doAjax(){
    $.ajax({
        url:'/crm/agentFinanceC/search.json',
        dataType:'json',
        data:{
            token:localStorage.getItem('crmtoken'),
            pageNu:page,
        },
        type:'post',
        success: function (data) {
            if(data.code==-1){
                window.parent.location.href="/crm/login.jsp"
            }
            if(data.code==0){
                alert(data.msg);
            }
            if(data.code==1){
                $('#record').html(data.datas.recordsTotal);
                $('#withdraw-record').empty();
                for(var i=0;i<data.datas.agentFinanceList.length;i++){
                    $('<tr class="gradeA odd">'+
                        '<a href=""><td class="sorting_1 withdrawId" style="text-decoration: underline;cursor: pointer;" data-toggle="modal" data-target="#myModal4" id="'+data.datas.agentFinanceList[i].id+'">'+data.datas.agentFinanceList[i].id+'</td></a>'+
                        '<td class=" ">'+data.datas.agentFinanceList[i].userName+'</td>'+
                        '<td class=" ">'+data.datas.agentFinanceList[i].realName+'</td>'+
                        '<td class="center ">'+data.datas.agentFinanceList[i].phone+'</td>'+
                        '<td class="sorting_1">'+data.datas.agentFinanceList[i].userState+'</td>'+
                        '<td class="sorting_1 initialize" style="text-decoration: underline;cursor: pointer;" initialize="'+data.datas.agentFinanceList[i].id+'">初始化</td>'+
                        '</tr>').appendTo($('#withdraw-record'));
                }

                //初始化密码
                $('.initialize').unbind('click').click(function(){
                    $.ajax({
                        url:'/crm/userC/initCRMPassword',
                        dataType:'json',
                        data:{
                            id:$(this).attr('initialize'),
                            token:localStorage.getItem('crmtoken'),
                        },
                        type:'post',
                        success:function(data){
                            if(data.code==-1){
                                window.parent.location.href="/crm/login.jsp"
                            }
                            if(data.code==0){
                                alert(data.msg)
                                return;
                            }
                            if(data.code==1){
                                alert('初始化成功');
                            }

                        }
                    })
                })

                //id处理
                $('.withdrawId').click(function(){
                    id = $(this).attr('id');
                    $.ajax({
                        url:'/crm/agentFinanceC/getOne.json',
                        dataType:'json',
                        data:{
                            token:localStorage.getItem('crmtoken'),
                            id:id,
                        },
                        type:'post',
                        success:function(data){
                            if(data.code==-1){
                                window.parent.location.href="/crm/login.jsp";
                            }
                            if(data.code==0){
                                alert(data.msg);
                            }
                            if(data.code==1){
                                $('#editagencyuserName').val(data.datas.realName);
                                $('#editagencyphone').val(data.datas.phone);
                                $('#editfinancePass').val(data.datas.password);
                                password = data.datas.password;
                                userkey = data.datas.userKey;
                            }
                        }
                    })
                })

                $(".pagination").createPage({
                    totalPage:data.datas.totalPage,
                    currPage:data.datas.pageNu,
                    pageCount:data.datas.pageNu,
                    turndown:'true',
                    backFn:function(p){
                        page=p;
                        doAjax();
                    }
                });
            }
        }
    })
}

$(function(){
    doAjax();
})

$('#editfinancePass').focus(function(){
    $('#editfinancePass').val('');
})

$('#editfinancePass').blur(function(){
    //$('#conPassBox').show();
    password = $('#editfinancePass').val();
    if(password==""){
        alert('密码不能为空')
    }else{
        password= calcMD5(password + userkey, 32);
    }
})

//编辑
$('#save2').click(function(){

    $.ajax({
        url:'/crm/agentFinanceC/toEdit.json',
        dataType:'json',
        data:{
            token:localStorage.getItem('crmtoken'),
            financeUserId:id,
            phone:$('#editagencyphone').val(),
            realName:$('#editagencyuserName').val(),
            password:password,
            userStateValue:$('#select').val(),
        },
        type:'post',
        success:function(data){
            if(data.code==-1){
                window.parent.location.href="/crm/login.jsp";
            }
            if(data.code==0){
                alert(data.msg);
            }
            if(data.code==1){
                alert('编辑成功');
                $('#myModal4').removeClass('fade in');
                $('#myModal4').addClass('fade');
                $('#myModal4').attr('aria-hidden',true);
                $('#myModal4').css('display','none');
                $('.modal-backdrop').css('display','none');
                page=1;
                doAjax();
            }
        }
    })
})

//检验登录账号
$('#agencyuserName').click(function(){
    var username = $('#agencyloginName').val();
    checkUser(username);
})

//检验手机号
$('#agencyphone').blur(function(){
    var mobile = $('#agencyphone').val();
    checkMobile(mobile);
})

//查询
$('#refer').click(function(){
    page=1;
    doAjax();
})


//点击关闭
$('.close,#close').click(function(){
    ifRefer = true;
})



//点击保存
$('#save').click(function(){

    var agencyloginName = $('#agencyloginName').val();
    var agencyuserName = $('#agencyuserName').val();
    var agencypassword = $('#financePass').val();
    var agencyconpassword = $('#confinancePass').val();
    var agencyphone = $('#agencyphone').val();
    if(!checkUser(agencyloginName)){
        return;
    }
    if(!checkMobile(agencyphone)){
        return;
    }
    if(agencypassword!=agencyconpassword){
        alert('两次输入的密码不相同');
        return;
    }
    $.ajax({
        url:'/crm/agentFinanceC/toAdd.json',
        dataType:'json',
        data:{
            token:localStorage.getItem('crmtoken'),
            userName:agencyloginName,
            realName:agencyuserName,
            password:agencypassword,
            phone:agencyphone,
        },
        type:'post',
        success:function(data){
            if(data.code==0){
                alert(data.msg)
                return;
            }
            if(data.code==-1){
                window.parent.location.href="/crm/login.jsp"
            }
            if(data.code==1){
                alert('添加成功');
                $('#myModal5').removeClass('fade in');
                $('#myModal5').addClass('fade');
                $('#myModal5').attr('aria-hidden',true);
                $('#myModal5').css('display','none');
                $('.modal-backdrop').css('display','none');
                $('#myModal5 input').val('');
                page=1;
                doAjax();
            }
        }
    })
})
